//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    banner:[],
    newsList:[],
    newsNum:0,
    swiperCurrent: 0
  },
  callPhone: function () {
    wx.makePhoneCall({
      phoneNumber: '13983643033'
    })
  },
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  onLoad: function () {
    var that=this;
    wx.request({
      url: app.globalData.apiUrl+'/banner2', //仅为示例，并非真实的接口地址
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        res.data.forEach(function(v,i){
          res.data[i].imgurl = app.globalData.resourceUrl + res.data[i].imgurl;
        })
        that.setData({
          banner: res.data
        });
      }
    });
    wx.request({
      url: app.globalData.apiUrl + '/major', //仅为示例，并非真实的接口地址
      data: {
        count:3
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        res.data.forEach(function (v, i) {
          res.data[i].mainimg = app.globalData.resourceUrl + res.data[i].mainimg;
        })
        that.setData({
          courseList: res.data
        });
      }
    })
    that.ajaxGetNews();
  },
  onReachBottom: function () {
    this.ajaxGetNews();
  },
  ajaxGetNews:function(){
    wx.showLoading({
      title: '加载中',
    });
    var that=this;
    wx.request({
      url: app.globalData.apiUrl + '/news', //仅为示例，并非真实的接口地址
      data: {
        start: that.data.newsNum,
        count: 2
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        
        var newarr = that.data.newsList;
        res.data.forEach(function (v, i) {
          res.data[i].mainimg = app.globalData.resourceUrl + res.data[i].mainimg;
          newarr.push(res.data[i]);
        })
        that.setData({
          newsList: newarr,
          newsNum: that.data.newsNum + res.data.length
        });
        wx.hideLoading();
        if (res.data.length<=0){
          wx.showToast({
            title: '没有更多新闻了',
            icon: 'success',
            duration: 1000
          })
        }
      }
    })
  }
})
